<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends Controller {
     
   
    public function index(){
        
    }
    public function register(){

    	$db=D('mall_login');
    	$data=$db->create();
    	$error=$db->getError();
    	if ($data==false) {
    		echo $error;
    	}else{
    		$db->add();
    		echo "注册成功";

    	}
    	
    }
    public function login(){
    	$user=I('post.user');
    	$password=I('post.password');	
    	$db=M('mall_login');

    	//判断是手机登录还是邮件登录
    	$data=$db->where("email='{$user}' and password='{$password}'")->find();
    	$data2=$db->where("phone='{$user}' and password='{$password}'")->find();
    	
    	if (isset($data) or isset($data2)) {
    		if (isset($data)) {
    			$_SESSION['uname']=$data['email'];
	    		$_SESSION['uid']=$data['id'];
	    		echo "登录成功";
    		}else{
    			$data=$data2;
    			$_SESSION['uname']=$data['phone'];
	    		$_SESSION['uid']=$data['id'];
	    		echo "登录成功";

    		}
    	}else{
    		echo "登录失败";
    	}
    	
    	

    }
    public function class1(){
        //栏目数据添加
        $db=M('column');
        $db->create();
        $db->add();

    }
    public function classSelect(){
        $fid=I('get.fid');
        $db=M('column');
        $ret=$db->where("fid={$fid}")->select();
        if ($ret!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $ret , 

                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 

                );
        }

        $this->ajaxReturn($data);
    }
    public function selectProduct(){
        $id=I('get.id');
        $db=M('product');
        $ret=$db->where("class='{$id}'")->select();
        foreach ($ret as $key => $value) {
            $value['images']=json_decode($value['images'],true);
            $img=$value['images'][0];
            $ret[$key]['images']=$img;
        }
        if ($ret!=false) {
            $data = array(
                'status'=>true,
                'count'=>0,
                'data'=>$ret
                );
        }else{
            $data = array(
                'status'=>false,
                'count'=>1,
                'data'=>"没有数据"
                );

        }
        $this->ajaxReturn($data);
       

    }
    public function property(){
        $name['name']=I('get.name');
        $name['fid']=0;
        $db=M('property');
        $ret=$db->where("name='{$name['name']}'")->find();
        if (empty($ret)==true) {
           $db->data($name)->add();
        }
        
    }
    public function propertySelect(){
        $fid=I('get.fid');
        $db=M('property');
        $ret=$db->where("fid={$fid}")->select();
        if ($ret!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $ret , 

                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 

                );
        }

        $this->ajaxReturn($data);
    }
    public function addproperty(){
            $db=M('property');
            $db->create();
            $db->add();
    }
     public function propertyShow(){
        $fid=I('get.fid');
        $db=M('property');
        $ret=$db->where("fid={$fid}")->select();
        if ($ret!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $ret , 

                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 

                );
        }

        $this->ajaxReturn($data);
    }
    public function shopCart(){
        //对传进来的属性进行转化处理
        $data['property']=$_GET['property'];
        $property=explode(";", $data['property']);
        $k=count($property);
        unset($property[$k-1]);
        $data['property']=json_encode($property, JSON_UNESCAPED_UNICODE );
        
        $data['uid']=$_SESSION['uid'];
        $data['name']=$_GET['name'];
        $data['pid']=$_GET['pid'];
        $data['images']=$_GET['images'];
        $data['price']=$_GET['price'];
        $data['originprice']=$_GET['originprice'];
        $data['count']=$_GET['count'];
        $db=M('shopcart');
        $exist=$db->where("name='{$data['name']}'")->find(); //判断是否加入购物车
        if (empty($exist)) {
            $db->data($data)->add();
        }else{
            $db->create($data);
            $db->where("id={$exist['id']}")->save();
        }
        $count=$db->where("uid={$data['uid']}")->select();
        $count=count($count);
        if ($count!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $count,         
                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 
                );
        }

        $this->ajaxReturn($data);
       
    }
    public function countChange(){
        $db=M('shopcart');
        $db->create();
        $data=$db->where("id={$_POST['id']}")->save();

    }
    public function gainShop(){
        $id=I('get.id');
        $db=M('shopcart');
        //先删除指定的商品
        if ($id==true) {
            $db->where("id={$id}")->delete();
        }
      
        //重新载入商品信息
        $ret=$db->select();
        foreach ($ret as $key => $value) {
           $value['property']=json_decode($value['property'],true);
           $ret[$key]['property']= $value['property'];
        }
        if ($ret!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $ret , 

                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 

                );
        }

        $this->ajaxReturn($data);

    }
     public function addAddress(){
        //地址数据添加
        $db=M('address');
        $db->create();
        $db->add();

    }
    public function addressSelect(){    //地址管理

        $fid=I('get.fid');
        $db=M('address');
        $ret=$db->where("fid={$fid}")->select();
        if ($ret!=false) {
            $data = array(
                'status' => true ,
                'count' => 0,  
                'data' => $ret , 

                );
        }else{
            $data = array(
                'status' => false ,
                'count' => 1,  
                'data' => "没有数据" , 

                );
        }

        $this->ajaxReturn($data);

    }
    //public function order(){               //定单管理
       // S('commonData',null);
       // $arrayId = array();
        //$arrayId = $_GET['idProduct'];
       // $db=M('shopcart');
       // $dc=M('order');
        //foreach ($arrayId as $key => $id) {
          // $data[]=$db->where("id={$id}")->find();//找到所选产品
          // $data['sid']=$data['id'];//获取购物车数据表的ID
          //unset($data['id']);
           //$dc->create($data);
           //$dc->add();          //把选择的商品添加到定单
           //$db->where("id={$id}")->delete();   //然后删除购物车的商品
          
       // }
       //S('commonData',$data);

    //}
     public function getOrData(){
        $id=I('get.id');
        $id=explode(",",$id);
        $db=M('shopcart');
        foreach ($id as $key => $value) {
            $ret[]=$db->where("id={$value}")->find();
        }
        foreach ($ret as $key => $value) {
            $value['property']=json_decode($value['property'],ture);
            $ret[$key]['property']= $value['property'];
        }

         if ($ret!=false) {
            $data = array(
                 'status'=>true,
                 'count'=>0,
                 'data'=>$ret

                );
        }else{
            $data = array(
                'status'=>false,
                'count'=>1,
                'data'=>"没有数据"
                );
        }
        $this->ajaxReturn($data);
    }
    public function deleteOrder(){      //关闭交易
        $arrayId=I('get.arrayId');
        print_r($arrayId);
        $db=M('order');
        foreach ($arrayId as $key => $id) {
            $ret=$db->where("id={$id}")->delete();
        }
       
        echo $ret;

    }
    public function clientAddress(){
        $id=$_POST['id'];
        if (!empty($id)) {
            $db=M('addressClient');
            $data=$db->create();
            $address=I('post.address','','');
            $address=explode(";",$address);
            $data['uid']=$_SESSION['uid'];
            $data['address']=json_encode($address,JSON_UNESCAPED_UNICODE);
            $ret=$db->where("id={$id}")->save($data);
            echo $ret;
        }else{
            $db=M('addressClient');
            $data=$db->create();
            $address=I('post.address','','');
            $address=explode(";",$address);
            $data['uid']=$_SESSION['uid'];
            $data['address']=json_encode($address,JSON_UNESCAPED_UNICODE);
            $data['default']="否";
            $ret=$db->add($data);
            echo $ret;
        }
    }
    public function getAddress(){
        $db=M('addressClient');
        $uid=$_SESSION['uid'];
        $ret=$db->where("uid={$uid}")->select();
        foreach ($ret as $key => $value) {
           $value['address']=json_decode($value['address'],true);
           $ret[$key]['address']=  $value['address'];
        };
        if ($ret!=false) {
            $data = array(
                'status'=>true,
                'count'=>0,
                'data'=>$ret
            );
        }else{
             $data = array(
                'status'=>false,
                'count'=>1,
                'data'=>"没有数据"
            );
        }
        $this->ajaxReturn($data);
    }
    function deletAddress(){            //删除地址
        $id=I('get.id');
        $db=M('addressClient');
        $db->where("id={$id}")->delete();
    }

    function chanDefault(){             //修改默认地址
        $id=I('get.id');
        $db=M('addressClient');
        $data=$db->where(array('default'=>'是'))->find();
        //$data=$db->where("default='是'")->find();
        //echo $db->getLastSql();
        //exit;
        if (!empty($data)) {
            $dataRecovery['default']="否";
            $db->where("id={$data['id']}")->save($dataRecovery);
        }
        $chanData['default']="是";
        $db->where("id={$id}")->save($chanData);
    }
    function addOrder(){

        $arrayId = array();
        $arrayId = $_GET['idProduct'];
        $db=M('shopcart');
        $dc=M('order');
        $time=time();
        $status=$_GET['status'];
        foreach ($arrayId as $key => $id) {
           $data = array();
           $data=$db->where("id={$id}")->find();//找到所选产品
           $data['sid']=$data['id'];//获取购物车数据表的ID
           unset($data['id']);
           $data['time']=$time;
           $data['status']=$status;
           $data['delivery']="未发货";
           $data['comment']="未评论";
           $data['urgent']="未加急";
           $data['custondelivery']="否";
           $dc->create($data);
           $dc->add();          //把选择的商品添加到定单
           $db->where("id={$id}")->delete();   //然后删除购物车的商品
          
        }

    }

    public function getPaIfor(){        // 获取未付款待收货的订单
        $uid=$_SESSION['uid'];
        $status=I('get.status');
        $delivery=I('get.delivery');
        $comment=I('get.comment');
        if ($delivery==false) {
          $delivery="未发货";
        };
        if ($comment==false) {
          $comment="未评论";
        }
        $db=M('order');
        $data=$db->where("uid={$uid}")->select();
        $timeArray = array( );
        foreach ($data as $key => $value) {

            if (!in_array($value['time'], $timeArray)) {
               array_push($timeArray,$value['time'] );
            }
        }
        foreach ($timeArray as $key => $time) {
            if ($_GET==false){
                $where = array(
                        'time' => $time, 
                        'status' => '已付款',
                        );
                $data=$db->where($where)->select();
            }else{
                $where = array(
                            'time' => $time, 
                            'status' => $status,
                            'delivery' => $delivery,
                            'comment' => $comment,
                            );
                $data=$db->where($where)->select();
            }
            foreach ($data as $k => $val) {
               $val['property']=json_decode($val['property'],true);
               $data[$k]['property']=$val['property'];
            }
            $orderArray[$key]['time']=$time;
            $orderArray[$key]['data']=$data;
            $orderArray[$key]['status']=$data[0]['status']; 
            $orderArray[$key]['delivery']=$data[0]['delivery'];            
        }
        rsort($orderArray);
        foreach ($orderArray as $key => $value) {
            if(empty($value['data'])){
                unset($orderArray[$key]);
            }
        }
        $ret=array_values($orderArray);
        if ($ret!=false) {
            $data = array(
                'status' => true,
                'count' => 0,
                'data' => $ret,

            );
        }else{
            $data = array(
                'status' => false,
                'count' => 1,
                'data' => "没有数据",

            );
        }
        
        $this->ajaxReturn($data);
    }

    function changeStatus(){            //改变定单的状态
            $arrayId=I('post.arrayId');
            //print_r($arrayId);
            $db=M('order');
            //$data['delivery']=I('post.delivery');
            $data=$db->create();
            foreach ($arrayId as $key => $id) {
                 $ret=$db->where("id={$id}")->save($data);
            }
           
            echo $ret;


    }
    function getComment(){      //获得未评论的的订单
        $uid=$_SESSION['uid'];
        $time=I('get.time');
        $where = array(
            'uid'=>$uid,
            'status' => '已付款',
            'delivery' =>'已收货',
            'comment' => '未评论',
            'time'=>$time
            );
        $db=M('order');
        $ret=$db->where($where)->order('time desc')->select();
        foreach ($ret as $key => $value) {
            $value['property']=json_decode($value['property'],true);
            $ret[$key]['property']=$value['property'];
        }
        if ($ret!=false) {
            $data = array(
                'status' => true,
                'count' => 0,
                'data' => $ret,

            );
        }else{
            $data = array(
                'status' => false,
                'count' => 1,
                'data' => "没有数据",

            );
        }
        
        $this->ajaxReturn($data);
    }
    function addComment(){
       

        //把评论添加到数据库
        $_POST['uid']=$_SESSION['uid'];
        $_POST['time']=time();
        $pid=I('post.pid');
        $time=I('post.buytime');
        $where = array(
            'pid' =>$pid ,
            'buytime'=>$time 
            );

        $db=M('comment');
        $data=$db->where($where)->find();
        $db->create();
        if (isset($data)) {
           $ret=$db->where("id={$data['id']}")->save();
        }else{
           $ret=$db->add();
        }
        if ($ret==true) {
             //修改定单状态
            $oid=I('post.oid');
            $dada['comment']="已评论";
            $dc=M('order');
            $dc->where("id={$oid}")->save($dada);
        }
        echo $ret;

    }
    function getComDeta(){          //后台获取评论
        $pid=I('get.pid');
        $db=M('comment');
        $ret=$db->where("pid={$pid}")->select();
        if (isset($ret)) {
            $dc=M('mallLogin');
            foreach ($ret as $key => $value) {
               $data=$dc->where("id={$value['uid']}")->find();
                 $name=$data['email'];
               if (!empty($data['name'])) {
                  $name=$data['name'];
               }
               if (!empty($data['email'])) {
                  $name=$data['email'];
               }
               if (!empty($data['phone'])) {
                  $name=$data['phone'];
               }
               $ret[$key]['name']=$name;
            }
            $dd=M('answercomment');
            foreach ($ret as $key => $value) {
               $dataAn=$dd->where("cid={$value['id']}")->find();
               $ret[$key]['answer']=$dataAn['content'];
            }
        }

        if ($ret!=false) {
            $data = array(
                'status' => true,
                'count' => 0,
                'data' => $ret,

            );
        }else{
            $data = array(
                'status' => false,
                'count' => 1,
                'data' => "没有数据",

            );
        }
        
        $this->ajaxReturn($data);

    }
    function pushAnCom(){           //上传回复的内容
        $uid=I('post.uid');
        $pid=I('post.pid');
        $cid=I('post.cid');
        $db=M('answercomment');
        $where = array(
            'uid' =>$uid , 
            'pid'=>$pid,
            'cid'=>$cid
            );
        $ret=$db->where($where)->find();
        if (empty($ret)) {
            $db->create();
            $row=$db->add(); 
        }else{
            $data=$db->create();
            $row=$db->where("id={$ret['id']}")->save($data);
        }
        echo $row;

    }
    public function getFoot(){      //获取用户足迹
        $uid=$_SESSION['uid'];
        $db=M('foot');
        $dataFoot=$db->where("uid={$uid}")->select();
        $dc=M('product');
        foreach ($dataFoot as $key => $value) {
            $data[]=$dc->where("id={$value['pid']}")->find();
        }
        foreach ($data as $key => $value) {
           $value['images']=json_decode($value['images'],true);
           $data[$key]['images']= $value['images'][0];
           $where = array(
                    'uid' =>$uid,
                    'pid'=>$value['id'] 
                    );
           $footId=$db->where($where)->find();
           $data[$key]['footId']=$footId['id'];
        }
        $ret=$data;
        if ($ret!=false) {
            $data = array(
                'status' => true,
                'count' => 0,
                'data' => $ret,

            );
        }else{
            $data = array(
                'status' => false,
                'count' => 1,
                'data' => "没有数据",

            );
        }
        
        $this->ajaxReturn($data);

    }
    public function deleteFoot(){
        $id=I('get.id');
        $db=M('foot');
        $ret=$db->where("id={$id}")->delete();
        echo $ret;
    }

}